package com.mdd.common.util;

/**
 * @author yangzhengyin
 * @date 2025/10/18
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Utility class for handling image operations
 */
public class ImageUtils {

    /**
     * Converts a list of image URLs to a comma-separated string
     * @param images List of image URLs
     * @return Comma-separated string of image URLs
     */
    public static String imageListToString(List<String> images) {
        if (images == null || images.isEmpty()) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < images.size(); i++) {
            sb.append(images.get(i));
            if (i < images.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    /**
     * Converts a comma-separated string of image URLs to a list
     * @param imageString Comma-separated image URLs
     * @return List of image URLs
     */
    public static List<String> stringToList(String imageString) {
        if (imageString == null || imageString.trim().isEmpty()) {
            return new ArrayList<>();
        }

        String[] imageArray = imageString.split(",");
        return Arrays.asList(imageArray);
    }

    /**
     * Converts a list of image URLs to a comma-separated string with relative URLs
     * @param images List of image URLs
     * @return Comma-separated string of relative image URLs
     */
    public static String imageListToRelativeString(List<String> images) {
        if (images == null || images.isEmpty()) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < images.size(); i++) {
            String relativeUrl = UrlUtils.toRelativeUrl(images.get(i));
            sb.append(relativeUrl);
            if (i < images.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }
}